﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace UniversityStudentManagemnetSystem.DAO.DAL.Gateway
{
    public class ScheduleGateway:DAO.Gateway.Gateway
    {
        public ScheduleGateway() : base()
        {
        }
        public bool ClassScheduleSave(CourseSchedule courseSchedule)
        {
            try
            {
                Connection.Open();
                string insertQuery =
                    string.Format(
                        "insert into t_ScheduleClass values (@DepartmentId,@SemesterId,@CourseId,@BuildingId,@RoomId,@Day,@Flag,@StartingTime,@EndingTime)");
                Command.CommandText = insertQuery;
                Command.Parameters.Clear();
                Command.Parameters.Add("@DepartmentId", SqlDbType.Int);
                Command.Parameters["@DepartmentId"].Value = courseSchedule.Department.DeptId;
                Command.Parameters.Add("@SemesterId", SqlDbType.Int);
                Command.Parameters["@SemesterId"].Value = courseSchedule.Semester.Id;
                Command.Parameters.Add("@CourseId", SqlDbType.Int);
                Command.Parameters["@CourseId"].Value = courseSchedule.Course.CourseId;
                Command.Parameters.Add("@BuildingId", SqlDbType.Int);
                Command.Parameters["@BuildingId"].Value = courseSchedule.Building.Id;
                Command.Parameters.Add("@RoomId", SqlDbType.Int);
                Command.Parameters["@RoomId"].Value = courseSchedule.Room.Id;
                Command.Parameters.Add("@Day", SqlDbType.VarChar);
                Command.Parameters["@Day"].Value = courseSchedule.Day;
                Command.Parameters.Add("@Flag", SqlDbType.VarChar);
                Command.Parameters["@Flag"].Value = "S";
                Command.Parameters.Add("@StartingTime", SqlDbType.VarChar);
                Command.Parameters["@StartingTime"].Value = courseSchedule.Times.StartingTime;
                Command.Parameters.Add("@EndingTime", SqlDbType.VarChar);
                Command.Parameters["@EndingTime"].Value = courseSchedule.Times.EndingTime;
                Command.ExecuteNonQuery();

            }
            catch (FormatException exception)
            {
                throw new Exception(exception.Message);
            }
            catch (Exception exception)
            {
                
                throw new Exception("Save Could Not Perform due To Sustem Error!",exception);
            }
            finally
            {
                if (Connection!=null && Connection.State==ConnectionState.Open)
                {
                    Connection.Close();
                }
            }
            return true;
            //if (nbrOfAffectedRows == (int)Rows.oneRowAffected)
            //{
            //    return true;
            //}
            //else
            //{
            //    return false;
            //}
        }


        internal DataTable GetAllScheduledCourseForId(int buildingId, int roomId, string day)
        {
           
            try
            {

                string selectQuery = string.Format("SELECT StartingTime, EndingTime FROM t_ScheduleClass WHERE BuildingId = '" + buildingId + "' AND RoomId ='" + roomId + "' AND Day ='" + day + "'");
                 Command.CommandText = selectQuery;
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectQuery, GetConnectionString());
                DataSet dataSet = new DataSet();
                sqlDataAdapter.Fill(dataSet);
                DataTable myDataTable = dataSet.Tables[0];
                nbrOfTableRows = myDataTable.Rows.Count;
                return myDataTable;
            }
            catch (FormatException exception)
            {
                throw new Exception(exception.Message);
            }
            catch (Exception exception)
            {
                
                throw new Exception("Database Error",exception);
            }
            
            
        }
        }
    
}